﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using WpfControlLibrary.Converts;

namespace WpfControlLibrary
{
    /// <summary>
    /// 按钮状态变化时事件数据
    /// </summary>
    public class ButtonStateChangedArgs : EventArgs
    {
        /// <summary>
        /// 无参构造器
        /// </summary>
        public ButtonStateChangedArgs() { }

        /// <summary>
        /// 带有一个参数的事件数据 OnToOff 或 OffToOn
        /// </summary>
        /// <param name="changeType">变化类型</param>
        public ButtonStateChangedArgs(string changeType)
        {
            ChangeType = changeType;
        }

        /// <summary>
        ///  OnToOff 或 OffToOn
        /// </summary>
        public string ChangeType { set; get; }
    }

    /// <summary>
    /// 状态信息 ON或OFF
    /// </summary>
    public enum States
    {
        ON,
        OFF
    }

    /// <summary>
    /// 可以左右移动的按钮
    /// </summary>
    public partial class OnOffButton : UserControl
    {
        /// <summary>
        /// 当按钮状态发生变化时，产生一个事件
        /// </summary>
        public event EventHandler<ButtonStateChangedArgs> StateChanged;

        public OnOffButton()
        {
            InitializeComponent();
        }
        #region 绑定属性

        //初始化滑块位置
        private void InitScrollPosition()
        {
            States currentState = (States)GetValue(ButtonStatesProperty);
            if (currentState == States.ON)
            {
                double width = (double)GetValue(MyHeightProperty);
                double scrollWidth = ChangeButton.ActualHeight;
                double left = width + (width - scrollWidth) / 2;
                ChangeButton.Margin = new Thickness(left,0,0,0);
            }
            else
            {
                double width = (double)GetValue(MyHeightProperty);
                double scrollWidth = ChangeButton.ActualHeight;
                double left = (width - scrollWidth) / 2;
                ChangeButton.Margin = new Thickness(left,0,0,0);
            }
        }

        //控件高度
        static DependencyProperty MyHeightProperty = DependencyProperty.Register(
            "MyHeight",
            typeof(double),
            typeof(OnOffButton)
        );

        //开关的状态
        static DependencyProperty ButtonStatesProperty = DependencyProperty.Register(
            "ButtonStates",
            typeof(States),
            typeof(OnOffButton),
            new PropertyMetadata(States.OFF)
            );
        #endregion

        //打开的动画
        private ThicknessAnimation MoveOnAnimation()
        {
            ThicknessAnimation thicknessAnimation = new ThicknessAnimation();
            thicknessAnimation.Duration = new Duration(new TimeSpan(0,0,0,0,100));
            double width = (double)GetValue(MyHeightProperty);
            double scrollWidth = ChangeButton.ActualHeight;
            double left = width + (width - scrollWidth) / 2;
            thicknessAnimation.To = new Thickness(left,0,0,0);
            return thicknessAnimation;
        }

        //关闭的动画
        private ThicknessAnimation MoveOffAnimation()
        {
            ThicknessAnimation thicknessAnimation = new ThicknessAnimation();
            thicknessAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100));
            double width = (double)GetValue(MyHeightProperty);
            double scrollWidth = ChangeButton.ActualHeight;
            double left = (width - scrollWidth)/2;
            thicknessAnimation.To = new Thickness(left, 0, 0, 0);
            return thicknessAnimation;
        }

        //点击时状态发生变化，并产生通知
        private void Mouse_Down(object sender,RoutedEventArgs args) {
            States currentState = (States)GetValue(ButtonStatesProperty);
            if (currentState==States.OFF) { 
                SetValue(ButtonStatesProperty,States.ON);
                ChangeButton.BeginAnimation(Ellipse.MarginProperty, MoveOnAnimation()); //滑动按钮
                ButtonStateChangedArgs buttonStateChangedArgs = new ButtonStateChangedArgs("OffToOn");
                StateChanged?.Invoke(this, buttonStateChangedArgs);
            } 
            else {
                SetValue(ButtonStatesProperty, States.OFF);
                ChangeButton.BeginAnimation(Ellipse.MarginProperty, MoveOffAnimation());
                ButtonStateChangedArgs buttonStateChangedArgs = new ButtonStateChangedArgs("OnToOff");
                StateChanged?.Invoke(this, buttonStateChangedArgs);
            } 

        }

        //尺寸发生变化时，刷新尺寸
        private void Size_Changed(object sender,SizeChangedEventArgs args) {
            SetValue(MyHeightProperty,Height); //刷新尺寸
            InitScrollPosition(); //刷新完尺寸，滑动滑块到需要的状态
        }

        /// <summary>
        /// 获取按钮当前状态
        /// </summary>
        /// <returns>按钮当前状态</returns>
        public States GetCurrentStates()
        {
            return (States)GetValue(ButtonStatesProperty);
        }

        /// <summary>
        /// 设置控件当前状态
        /// </summary>
        /// <param name="states">状态</param>
        public void SetCurrentState(States states) {
            SetValue(ButtonStatesProperty, states);
        }

    }
}
